package choreganizer;

import java.io.*;
import java.util.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipException;


public class MyReaders
{
	private File databaseFile;
	
	
	
	public MyReaders(File databaseFile)
	{
		this.databaseFile = databaseFile;
	}
	
	
	public ChoreSchedule[] getChoreSchedules(int matchMonth, int matchYear)
	throws IOException, ZipException
	{
		ChoreSchedule[] choreSchedules = new ChoreSchedule[0];
		
		// Open our zip file
		ZipFile zip = new ZipFile( databaseFile );
		
		// Get the entries for the xml files
		ZipEntry choreEntry = zip.getEntry("chores.xml");
		ZipEntry schedEntry = zip.getEntry("schedules.xml");
		
		// Create streams from the zip entries
		InputStream choreStream = zip.getInputStream( choreEntry );
		InputStream schedStream = zip.getInputStream( schedEntry );
			
		try 
		{
			// Create XML parser
			XMLReader parser = XMLReaderFactory.createXMLReader();
			
			// Create instance of ScheduleReader and get any scheduled items that match the year/month
			ScheduleReader schedReader = new ScheduleReader( matchMonth, matchYear );
			parser.setContentHandler( schedReader );
			parser.parse( new InputSource(schedStream) );
			
			// Get the ChoreSchedule vector, and convert it to an array
			choreSchedules = schedReader.getChoreSchedules().toArray( new ChoreSchedule[0] );
			
			// If we found any scheduled items, we'll create an instance of ChoreReader.  We'll then
			// pass in the ChoreDataWrapper array to it, and it'll match the Chore to the Schedule,
			// and put the chore details in the ChoreData object
			ChoreReader choreReader = null;
			if (choreSchedules.length > 0)
			{
				choreReader = new ChoreReader( choreSchedules );
				parser.setContentHandler( choreReader );
				parser.parse( new InputSource(choreStream) );
			}
			
			// Cleanup
			choreStream.close();
			schedStream.close();
			zip = null;
			choreEntry = null;
			schedEntry = null;
			parser = null;
			schedReader = null;
			choreReader = null;
		}
		
		// The only errors here we will really see are from empty xml files, so we'll just
		// ignore them all together
		catch (SAXException se) 
		{
			//System.err.println("Error in MyReaders.getChoreSchedules()");
			//System.err.println(se);
		}
		finally
		{
			return choreSchedules;
		}
	}
	
	
	public ChoreData[] getAllChores()
	{
		ChoreData[] chores = new ChoreData[0];
		try 
		{
			// Open our zip file
			ZipFile zip = new ZipFile( databaseFile );
			
			// Get the chore entry
			ZipEntry choreEntry = zip.getEntry("chores.xml");
			// Create stream from the zip entry
			InputStream choreStream = zip.getInputStream( choreEntry );
			
			// Create XML parser
			XMLReader parser = XMLReaderFactory.createXMLReader();
			
			// Get all chores
			ChoreReader choreReader = new ChoreReader( true );
			parser.setContentHandler( choreReader );
			parser.parse( new InputSource(choreStream) );
			
			// Get the ChoreDataWrapper vector, and convert it to an array
			chores = choreReader.getAllChores().toArray( new ChoreData[0] );
			
			// Cleanup
			choreReader = null;
			parser = null;
			choreStream.close();
			choreStream = null;
			choreEntry = null;
			zip = null;
		} 
		catch (Exception e) 
		{
			System.err.println( e.getMessage() );
		}
		finally 
		{
			return chores;
		}
	}
	
	
	public ScheduleData[] getAllSchedules()
	{
		ScheduleData[] schedules = new ScheduleData[0];
		try 
		{
			// Open our zip file
			ZipFile zip = new ZipFile( databaseFile );
			
			// Get the chore entry
			ZipEntry schedEntry = zip.getEntry("schedules.xml");
			// Create stream from the zip entry
			InputStream schedStream = zip.getInputStream( schedEntry );
			
			// Create XML parser
			XMLReader parser = XMLReaderFactory.createXMLReader();
			
			// Get all schedules
			ScheduleReader schedReader = new ScheduleReader( true );
			parser.setContentHandler( schedReader );
			parser.parse( new InputSource(schedStream) );
			
			// Get the ChoreDataWrapper vector, and convert it to an array
			schedules = schedReader.getAllSchedules().toArray( new ScheduleData[0] );
			
			// Cleanup
			schedReader = null;
			parser = null;
			schedStream.close();
			schedStream = null;
			schedEntry = null;
			zip = null;
		} 
		catch (Exception e) 
		{
			System.err.println( e.getMessage() );
		}
		finally 
		{
			return schedules;
		}
	}
	
	

}
